$(function () {
    $("#backPage").load("/html/backPage.html");
})

// 查询所有权限
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = jsonGetValue("/permission/getSelectList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
// 向权限列表中填充数据
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            '        <td><input type="checkbox" name="checkOne" value="' + data[i].id + '"></td>\n' +
            "        <td>" + data[i].id + '</td>\n' +
            "        <td>" + data[i].name + "</td>\n" +
            "        <td>" + data[i].value + "</td>\n";
        if (data[i].stateCode == "enable") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].id + "','disable')\"><span>" + data[i].permissionState + "</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].stateCode == "disable") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].id + "','enable')\"><span>" + data[i].permissionState + "</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
        html += "    <td>" + data[i].createTime + "</td>\n" +
            "        <td>" + data[i].updateTime + "</td>\n";
        html += "    <td>\n" +
            "            <div onclick=\"edit('"+ data[i].id +"')\">" +
            '            <a href="#edit-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-edit">&#xe608;</i></a></div>\n' +
            "            <div onclick=\"del('"+ data[i].id +"')\">" +
            '            <a href="#del-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-del">&#xe60b;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }
    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(5) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
}

// 修改权限状态
function changeShowState(id,stateCode){
    let data = {
        id: id,
        stateCode:stateCode
    };
    let result = jsonPost("/permission/updateState", data)
    if (result.data.result == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 添加
function addPermission() {
    let name =  $("#addName").val();
    let value = $("#addValue").val();
    let data = {
        name:name,
        value: value,
    };
    if (name == null || name == "") {
        $("#nameTip").text("权限名不能为空");
    } else if (value == null || value == "") {
        $("#nameTip").text();
        $("#valueTip").text("权限值不能为空");
    } else {
        let result = jsonPost("/permission/addPermission", data);
        console.log(result);
        if (result.data.result == "success") {
            $(".right").load("/html/permission/permission-list.html");
        }
    }
}

// 根据id获取权限
function getPermissionById(id) {
    let data = {id: id}
    let result = jsonGetValue("/permission/getPermissionById", data);
    console.log(result);
    setPermissionDataById(result.data.data);
}
// 给修改和删除模态框赋值
function setPermissionDataById(data) {
    $("#updateId").val(data.id);
    $("#updateName").val(data.name);
    $("#updateValue").val(data.value);
    $("#delId").val(data.id);
    $("#tips").html("是否删除权限:" + data.name + "?");
}

// 弹出修改模态框
function edit(id) {
    getPermissionById(id);
}
// 执行修改
function doEdit() {
    let id = $("#updateId").val();
    let name =  $.trim($("#updateName").val());
    let value = $("#updateValue").val();
    let data = {
        id:id,
        name:name,
        value: value,
    };
    console.log(data);
    let result = jsonPost("/permission/updatePermission", data);
    console.log(result);
    if (result.data.result == 1) {
        $(".right").load("/html/permission/permission-list.html");
    }
}

// 弹出修改模态框
function del(id) {
    getPermissionById(id);
}
// 执行删除
function doDel() {
    let data = {id:$("#delId").val()}
    let result = jsonPost("/permission/delPermission", data);
    if (result.data.result == 1) {
        $(".right").load("/html/permission/permission-list.html");
    }
}

// 查询点击事件
function select() {
    page('home');
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}